package com.imoresoft.magic.view;

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ambitor.grass.sql.util.ConnectionUtils;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.IData;
import com.imoresoft.magic.exception.SessionExpiredException;
import com.imoresoft.magic.property.AppConstants;
import com.imoresoft.magic.property.SessionConstants;
import com.imoresoft.magic.util.DateUtil;

public class PageData {

	private Connection conn;

	private String time = null;

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
	 * 正常情况中不可以通过pageData获取实例，理论上它应该作为BaseAction的内部类
	 * 
	 * @throws Exception
	 */
	public PageData() throws Exception {
	}

	public Connection getConn(String jndi) throws SQLException {
		if (conn == null) {
			conn = ConnectionUtils.getConnection(jndi);
			conn.setAutoCommit(false);// 默认不自动提交，通过拦截器统一处理
		}
		return conn;
	}

	public Connection getConn() throws SQLException {
		if (conn == null) {
			conn = getConn(getDefaultJNDIName());
		}
		return conn;
	}

	public void setConn(Connection conn) {
		this.conn = conn;
	}

	private String getDefaultJNDIName() {
		return AppConstants.DEFAULT_JNDI_NAME;
	}

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public HttpSession getSession() {
		return ServletActionContext.getRequest().getSession(true);
	}

	public ServletContext getApplication() {
		return ServletActionContext.getServletContext();
	}

	public Object getRequestAttribute(String attrName) {
		return this.getRequest().getAttribute(attrName);
	}

	public void setRequestAttribute(String attrName, Object value) {
		this.getRequest().setAttribute(attrName, value);
	}

	public String getRequestParameter(String attrName) {
		return this.getRequest().getParameter(attrName);
	}

	public void RemoveRequestAttribute(String attrName) {
		this.getRequest().removeAttribute(attrName);
	}

	public Object getSessionAttribute(String attrName) {
		return this.getSession().getAttribute(attrName);
	}

	public void setSessionAttribute(String attrName, Object value) {
		this.getSession().setAttribute(attrName, value);
	}

	public void RemoveSessionAttribute(String attrName) {
		this.getSession().removeAttribute(attrName);
	}

	public Object getApplicationAttribute(String attrName) {
		return this.getApplication().getAttribute(attrName);
	}

	public void setApplicationAttribute(String attrName, Object value) {
		this.getApplication().setAttribute(attrName, value);
	}

	public void RemoveApplicationAttribute(String attrName) {
		this.getApplication().removeAttribute(attrName);
	}

	public IData getUserSession() {
		Object object = this.getSessionAttribute(SessionConstants.SESSION_TOP);
		if (object != null)
			return (IData) object;
		return new DataMap();
	}

	public String getTopSessionId() {
		Object object = this.getSessionAttribute(SessionConstants.SESSION_TOP);
		if (object != null)
			return ((IData) object).getString("SESSION_ID", "");
		return "";
	}

	public String getTopUserId() {
		Object object = this.getSessionAttribute(SessionConstants.SESSION_TOP);
		if (object != null)
			return ((IData) object).getString("USER_ID", "");
		return "";

	}

	public String getTopNick() {
		Object object = this.getSessionAttribute(SessionConstants.SESSION_TOP);
		if (object != null)
			return ((IData) object).getString("NICK", "");
		return "";
	}

	public String getUserRuleType() {
		Object object = this.getSessionAttribute(SessionConstants.SESSION_TOP);
		if (object != null)
			return ((IData) object).getString("RULE_TYPE", "");
		return null;
	}

	@SuppressWarnings("unchecked")
	public void setUserRuleType(String ruleType) throws SessionExpiredException {
		Object object = this.getSessionAttribute(SessionConstants.SESSION_TOP);
		if (object == null)
			throw new SessionExpiredException();
		((IData) (object)).put("RULE_TYPE", ruleType);
	}

	public void sendRedirect(String url) {
		try {
			this.getResponse().sendRedirect(url);
		} catch (IOException e) {
			logger.warn("跳转到" + url + "发生异常", e);
		}
	}

	public void close() throws SQLException {
		if (conn != null) {
			conn.close();
			logger.info("connection closed.");
		}
	}

	public void commit() throws SQLException {
		if (conn != null) {
			conn.commit();
			logger.info("connection commited.");
		}
	}

	public void rollback() throws SQLException {
		if (conn != null) {
			conn.rollback();
			logger.info("connection rollbacked.");
		}
	}

	public String getTime() {
		if (time == null)
			time = DateUtil.formatDate(new Date());
		return time;
	}

	public String getNewTime() {
		return DateUtil.formatDate(new Date());
	}
}